package com.example.konwRice;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import android.widget.Button;

import com.example.konwRice.index.Util.DataUtil;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;

import com.example.konwRice.databinding.ActivityMainBinding;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class MainActivity extends AppCompatActivity {
    private AppBarConfiguration appBarConfiguration; //抽屉控制条
    private NavController navController;//导航控制器
    private AppBarConfiguration mAppBarConfiguration;
    private ActivityMainBinding binding;
    private NavigationView navigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DrawerLayout drawerLayout=findViewById(R.id.drawer_layout);
        navController = Navigation.findNavController(this, R.id.fragmentContainerView2);
        // Navigation Fragment 集合
        Set<Integer> navigationFragment = new HashSet<>();
        navigationFragment.add(R.id.search);
        navigationFragment.add(R.id.homePage);
        navigationFragment.add(R.id.collection);
        navigationFragment.add(R.id.opinion);
        navigationFragment.add(R.id.share);
        navigationFragment.add(R.id.version);
        navigationFragment.add(R.id.browsingHistory);
        //以下是网络上参考代码
        if(navigationFragment!=null) {
            appBarConfiguration = new AppBarConfiguration
                    .Builder(navigationFragment)
                    .setOpenableLayout(drawerLayout)
                    .build();
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    DataUtil.getTeaData("",MainActivity.this);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        // 将 navigationView 和 navController 关联
        // navigationView 中的 menu 将会自动根据 navigation.xml 中的各 id 来绑定对应的 Fragment
        NavigationView navigationView = findViewById(R.id.navigation);
        NavigationUI.setupWithNavController(navigationView, navController);

    }



    @Override
    public boolean onSupportNavigateUp() {
        return NavigationUI.navigateUp(navController, appBarConfiguration) || super.onSupportNavigateUp();
    }


}